#ifndef __SPI_H
#define __SPI_H

/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"

#define SPI_SCK_PIN                     SCLK_Pin
#define SPI_SCK_GPIO_PORT               SCLK_GPIO_Port
#define SPI_MOSI_PIN                    MOSI_Pin
#define SPI_MOSI_GPIO_PORT              MOSI_GPIO_Port
#define SPI_MISO_PIN                    SDO_Pin
#define SPI_MISO_GPIO_PORT							SDO_GPIO_Port
#define SPI_CLR_PIN											CLR_Pin
#define SPI_CLR_GPIO_PORT								CLR_GPIO_Port



 
#define SPI_SCK_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()
#define SPI_MOSI_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()


#define MOSI_H  HAL_GPIO_WritePin(SPI_MOSI_GPIO_PORT, SPI_MOSI_PIN, GPIO_PIN_SET)  
#define MOSI_L  HAL_GPIO_WritePin(SPI_MOSI_GPIO_PORT, SPI_MOSI_PIN, GPIO_PIN_RESET)  
#define SCK_H   HAL_GPIO_WritePin(SPI_SCK_GPIO_PORT, SPI_SCK_PIN, GPIO_PIN_SET)  
#define SCK_L   HAL_GPIO_WritePin(SPI_SCK_GPIO_PORT, SPI_SCK_PIN, GPIO_PIN_RESET)  
#define CLR_H   HAL_GPIO_WritePin(SPI_CLR_GPIO_PORT, SPI_CLR_PIN, GPIO_PIN_SET) 
#define CLR_L   HAL_GPIO_WritePin(SPI_CLR_GPIO_PORT, SPI_CLR_PIN, GPIO_PIN_RESET)
#define LDAC_H  HAL_GPIO_WritePin(LDAC_GPIO_Port, LDAC_Pin, GPIO_PIN_SET)
#define LDAC_L  HAL_GPIO_WritePin(LDAC_GPIO_Port, LDAC_Pin, GPIO_PIN_RESET)
#define SYNC_H  HAL_GPIO_WritePin(SYNC_GPIO_Port, SYNC_Pin, GPIO_PIN_SET)
#define SYNC_L  HAL_GPIO_WritePin(SYNC_GPIO_Port, SYNC_Pin, GPIO_PIN_RESET)


#define MISO    HAL_GPIO_ReadPin(SPI_MISO_GPIO_PORT, SPI_MISO_PIN) 

/* AD5754R Register Map */
#define AD5754R_REG_DAC             0x00 // DAC register
#define AD5754R_REG_RANGE_SELECT    0x01 // Output range select register
#define AD5754R_REG_POWER_CONTROL   0x02 // Power control register
#define AD5754R_REG_CONTROL         0x03 // Control register

/* AD5754R Channel Address */
#define AD5724R_DAC_A               0x01 // Address of channel A 
#define AD5724R_DAC_B               0x02 // Address of channel B
#define AD5724R_DAC_C               0x04 // Address of channel C
#define AD5724R_DAC_D               0x08 // Address of channel D
#define AD5724R_DAC_ALL             0x0F // All four DACs 

/* AD5754R Range Bits */
#define AD5754R_UNIPOLAR_5_RANGE    0x00 // 0..+5(V)        
#define AD5754R_UNIPOLAR_10_RANGE   0x01 // 0..+10(V)       
#define AD5754R_UNIPOLAR_10_8_RANGE 0x02 // 0..+10.8(V)     
#define AD5754R_BIPOLAR_5_RANGE     0x03 // -5..+5(V)       
#define AD5754R_BIPOLAR_10_RANGE    0x04 // -10...+10(V)    
#define AD5754R_BIPOLAR_10_8_RANGE  0x05 // -10.8...+10.8(V) 

/* Control Functions */
#define AD5754R_NOP_OPERATION       0x00 // No operation instruction used in readback operations.
#define AD5754R_SDO_DISABLE         0x11 // Set by the user to disable the SDO output.
#define AD5754R_CLR_SELECT          0x12 // Selects the clear code: 0V, Midscale or Negative full scale.
#define AD5754R_CLAMP_ENABLE        0x14 // Set by the user to enable the current-limit clamp.
#define AD5754R_TSD_ENABLE          0x18 // Set by the user to enable the thermal shutdown feature.
#define AD5754R_CLEAR               0x40 // Sets the DAC registers to the clear code and updates the outputs.
#define AD5754R_LOAD                0x50 // Updates the DAC registers and, consequently, the DAC outputs.


#define u8 uint8_t
#define SIMULATE_DELAY_US __nop()
u8 simulate_spi_read_byte(void);
void simulate_spi_write_byte(u8 data);

void SPI_Init(void);
unsigned char SPI_SendByte(unsigned char dt);
unsigned char AD5724R_Init(void);			    
void AD5724R_RangeSelect();
void AD5724R_LoadDac(unsigned char dacChannel, int dacValue);
unsigned char AD5724R_GetRegisterValue(uint8_t r1,uint8_t r2,uint8_t r3);
void AD5724R_SetPower(unsigned char dacChannel);

/* Reads alert bits form Power Control Register. */
unsigned char AD5754R_ReadAlertBits(void);

void AD5724R_SetRegisterValue(uint8_t r1,uint8_t r2,uint8_t r3);

unsigned char SPI_SB(unsigned char dt);
#endif
